//package com.bbzn.device.client.config;
//
//import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.integration.annotation.IntegrationComponentScan;
//import org.springframework.integration.annotation.ServiceActivator;
//import org.springframework.integration.channel.DirectChannel;
//import org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory;
//import org.springframework.integration.mqtt.core.MqttPahoClientFactory;
//import org.springframework.integration.mqtt.outbound.MqttPahoMessageHandler;
//import org.springframework.messaging.MessageChannel;
//import org.springframework.messaging.MessageHandler;
//
//import javax.annotation.Resource;
//
///**
// * @Author wulongbo
// * @Date 2020/12/29 11:38
// * @Version 1.0
// */
//
///**
// * EMQX配置工具类
// */
//@Configuration
//@IntegrationComponentScan //消息扫描件
////@Slf4j
//public class EmqxMqttPublishConfig {
//
//    @Resource
//    private EmqxMqttProperties emqxMqttProperties;
//
//    /**
//     * MQTT的连接
//     */
////    @Bean
////    public MqttConnectOptions mqttConnectOptions() {
////        // 设置相关的属性
////        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
////        mqttConnectOptions.setUserName(emqxMqttProperties.getUsername());
////        mqttConnectOptions.setPassword(emqxMqttProperties.getPassword().toCharArray());
////        mqttConnectOptions.setServerURIs(new String[]{emqxMqttProperties.getHostUrl()});
////        // 心跳
////        mqttConnectOptions.setKeepAliveInterval(emqxMqttProperties.getKeepAlive());
////        mqttConnectOptions.setMqttVersion(emqxMqttProperties.getVersion());
////        mqttConnectOptions.setConnectionTimeout(emqxMqttProperties.getTimeout());
////        // 保留/清空曾经连接的客户端信息
////        mqttConnectOptions.setCleanSession(false);
////        // qos
////        String playload = "设备已断开连接";
////        // 遗嘱消息
////        mqttConnectOptions.setWill("last_topic", playload.getBytes(), emqxMqttProperties.getQos(), false);
////        return mqttConnectOptions;
////    }
//
//    /**
//     * paho factory,mqtt自定义的连接放入factory工厂中
//     */
//    @Bean
//    public DefaultMqttPahoClientFactory mqttPahoClientFactory() {
//        DefaultMqttPahoClientFactory defaultMqttPahoClientFactory = new DefaultMqttPahoClientFactory();
//        defaultMqttPahoClientFactory.setUserName(emqxMqttProperties.getUsername());
//        defaultMqttPahoClientFactory.setPassword(emqxMqttProperties.getPassword());
//        defaultMqttPahoClientFactory.setServerURIs(new String[]{emqxMqttProperties.getHostUrl()});
//        // 心跳
//        defaultMqttPahoClientFactory.setKeepAliveInterval(emqxMqttProperties.getKeepAlive());
//        defaultMqttPahoClientFactory.setConnectionTimeout(emqxMqttProperties.getTimeout());
//        // 保留/清空曾经连接的客户端信息
//        defaultMqttPahoClientFactory.setCleanSession(false);
//        // qos
//        String playload = "设备已断开连接";
//        // 遗嘱消息
//        defaultMqttPahoClientFactory.setWill(new DefaultMqttPahoClientFactory.Will("last_topic", playload.getBytes(), emqxMqttProperties.getQos(), false));
//        return defaultMqttPahoClientFactory;
//    }
//
//
//    /**
//     * 开启连接通道
//     */
//    @Bean(name = Constants.MQTT_PUBLISH_CHANNEL)
//    public MessageChannel getMqttPublishMessageChannel() {
//        DirectChannel directChannel = new DirectChannel();
//        return directChannel;
//    }
//
//
////    /**
////     * 开启连接通道
////     */
////    @Bean(name = Constants.MQTT_SUBSCRIBE_CHANNEL)
////    public MessageChannel getMqttSubscribeMessageChannel() {
////        DirectChannel directChannel = new DirectChannel();
////        return directChannel;
////    }
////
////
////
////    /**
////     * 监听topic.订阅者，消费者
////     */
////    @Bean
////    public MessageProducer inbound() {
////        MqttPahoMessageDrivenChannelAdapter mqttPahoMessageDrivenChannelAdapter = new MqttPahoMessageDrivenChannelAdapter(
////                emqxMqttProperties.getClientId() + "_wlb", mqttPahoClientFactory(), emqxMqttProperties.getDefaultTopic().split(",")
////        );
////        mqttPahoMessageDrivenChannelAdapter.setDisconnectCompletionTimeout(emqxMqttProperties.getTimeout());
////        mqttPahoMessageDrivenChannelAdapter.setConverter(new DefaultPahoMessageConverter());
////        mqttPahoMessageDrivenChannelAdapter.setQos(emqxMqttProperties.getQos());
////        mqttPahoMessageDrivenChannelAdapter.setOutputChannel(getMqttPublishMessageChannel());
////        return mqttPahoMessageDrivenChannelAdapter;
////    }
//
//    /**
//     * 订阅者，消费者
//     */
//    @Bean
//    @ServiceActivator(inputChannel = Constants.MQTT_PUBLISH_CHANNEL)
//    public MessageHandler getPubMessageHandler() {
//        MqttPahoMessageHandler mqttPahoMessageHandler = new MqttPahoMessageHandler(emqxMqttProperties.getClientId(), mqttPahoClientFactory());
//        mqttPahoMessageHandler.setAsync(true);
//        mqttPahoMessageHandler.setDefaultQos(emqxMqttProperties.getQos());
//        mqttPahoMessageHandler.setDefaultTopic(emqxMqttProperties.getDefaultTopic());
//        return mqttPahoMessageHandler;
//    }
//
//}
